package wangjg.sso;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class RemoteLogin {
	
	public Object login(String uri, String token){
		return get(uri,token);
	}
	
	private Object get(String uri, String token){
		HttpURLConnection connection=null;
		try{
			URL url = new URL(uri);
			connection = (HttpURLConnection) url.openConnection();
			connection.addRequestProperty("Cookie", token);
			connection.connect();
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			StringBuilder sb=new StringBuilder();
			char[] buf=new char[1024];
			int n=0;
			while ((n=reader.read(buf))!=-1) {
				sb.append(buf,0,n);
			}
			reader.close();
			connection.disconnect();
			return sb.toString();
		}catch(Exception e){
			return null;
		}finally{
			if(connection!=null)
				connection.disconnect();
		}
	}

	public Object logout(int servid, String token) throws IOException {
		return null;
	}
	
	public static void main(String args[]) throws Exception{
		String uri="http://www.wootone.com/user/person/updatePersonalDetailsInput";
		String token="JSESSIONID=030B0962728195890D5675B769098D03";
		RemoteLogin rl=new RemoteLogin();
		String s=rl.login(uri, token).toString();
		System.out.println(s);
	}
}
